<tr>
    <th>服务时间：</th>
    <td>
        <div class="table-responsive" id="preorderBox">
            <table class="table">
                <thead id="preorderBaseHead"></thead>
                <!--时间配置标题模板-->
                <script id="preorderHeadTemplate" type='text/html'>
                    <tr>
                        <%for(var item in templateData){%>
                        <td><%=templateData[item]['name']%></td>
                        <%}%>
                        <td>开始时间</td>
                        <td>结束时间</td>
                        <td>价格</td>
                        <td>库存</td>
                    </tr>
                </script>
                <tbody id="preorderBaseBody"></tbody>
                <!--时间配置内容模板-->
                <script id="preorderRowTemplate" type="text/html">
                    <%var i=0;%>
                    <%for(var item in templateData){%>
                    <%item = templateData[item]%>
                    <tr class="preorderTr<%=i%>">
                        <%var specArrayList = typeof item['spec_array'] == 'string' && item['spec_array'] ? JSON().parse(item['spec_array']) : item['spec_array'];%>
                        <%for(var result in specArrayList){%>
                        <%result = specArrayList[result]%>
                        <td>
                            <%if(result['type'] == 1){%>
                            <%=result['value']%>
                            <%}else{%>
                            <img class="img-thumbnail" width="30px" height="30px" src="<%=webroot(result['value'])%>">
                            <%}%>
                        </td>
                        <%}%>
                        <td>
                            <input type="hidden"  name="_preorder_goods_no[<%=i%>][]" value="<%=item['_preorder_goods_no']%>">
                            <div class="input-group col-md-6">
                                <input class="form-control" pattern='date' type="text" name="_preorder_start_time[<%=i%>][]" id="_preorder_start_time[<%=i%>][]" onfocus="WdatePicker({minDate:minDataSetting(this,'start')})" value="<%=item['_preorder_start_time']%>" />
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                            </div>
                        </td>
                        <td>
                            <div class="input-group col-md-6">
                                <input class="form-control" pattern='date' type="text" name="_preorder_end_time[<%=i%>][]"  onfocus="WdatePicker({minDate:minDataSetting(this,'end')})" value="<%=item['_preorder_end_time']%>" />
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                            </div>
                        </td>
                        <td>
                            <input type='hidden' name="_preorderGroupPrice[<%=i%>][]" value="<%=item['_preorderGroupPrice']%>" />
                            <input class="form-control input-sm" name="_preorder_price[<%=i%>][]" type="text" pattern="float" value="<%=item['_preorder_price']%>" style="width:70px;display:inline;" />
                            <button class="btn btn-sm <%if(item['_preorderGroupPrice']){%>btn-success<%}else{%>btn-default<%}%>" type="button" onclick="memberPreorderPrice(this,0,'<%=i%>');"><i class="fa fa-user"></i> 会员价格</button>
                        </td>
                        <td><input class="form-control input-sm" name="_preorder_store_nums[<%=i%>][]" type="text" pattern="int" value="<%=item['_preorder_store_nums']?item['_preorder_store_nums']:100%>" style="width:70px" /></td>
                        <td>
                            <a onclick="addPreorder(this);"><i class='operator fa fa-plus'></i></a>
                            <a onclick="delPreorder(this,'<%=i%>');"><i class='operator fa fa-close'></i></a>
                        </td>
                    </tr>
                    <%i++;%>
                    <%}%>
                </script>
            </table>
        </div>
        <p class="help-block">基于【基本数据】的时间段数据，修改基本数据可批量应用于本时间段</p>
    </td>
</tr>

<script language="javascript">
    initPreorderTable();

    $(document).ready(function(){

        $("body").on('change','input[name^="_goods_no"]',function(){
            var index = $(this).attr('name').substr(-2,1);
            $('input[name="_preorder_goods_no['+index+'][]"]').val($(this).val());
        });

        $("body").on('change','input[name^="_store_nums"]',function(){
            var index = $(this).attr('name').substr(-2,1);
            $('input[name="_preorder_store_nums['+index+'][]"]').val($(this).val());
        });

        $("body").on('change','input[name^="_sell_price"]',function(){
            var index = $(this).attr('name').substr(-2,1);
            $('input[name="_preorder_price['+index+'][]"]').val($(this).val());
        });

        $("body").on('change',':hidden[name^="_groupPrice"]',function(){
            var index = $(this).attr('name').substr(-2,1);
            $(':hidden[name="_preorderGroupPrice['+index+'][]"]').val($(this).val());
        });

    });

    //时间关联设置
    function minDataSetting(self,type) {
        var i = $(self).attr('name').substr(-4,1); //不同规格的索引
        var index = $("input[name='"+$(self).attr('name')+"']").index($(self)); //同规格列表的索引
        var lastTime = '';
        if (index == 0 && type == 'start'){
            return '%y-%M-%d';
        }
        if (type == 'end'){
            lastTime = $(self).parent().parent().prev().find('div').find('input[name="_preorder_start_time['+i+'][]"]').val();
            return lastTime;
        }
        if (index > 0 && type == 'start'){
            lastTime = $(self).parent().parent().parent().prev().find('td').find('input[name="_preorder_end_time['+i+'][]"]').val();
            lastTime = lastTime.replace(/-/g, '/');
            var date = new Date(lastTime);
            date.setDate(date.getDate() + 1);
            var year = date.getFullYear();
            var month =(date.getMonth() + 1).toString();
            var day = (date.getDate()).toString();
            if (month.length == 1) {
                month = "0" + month;
            }
            if (day.length == 1) {
                day = "0" + day;
            }
            var dateTime = year + "-" + month + "-" + day;
            return dateTime;
        }
    }

    //删除时间类商品规格
    function delPreorder(_self,i)
    {
        if ($('[name="_preorder_goods_no['+i+'][]"]').length == 1){
            alert('该规格数据必须保留一个');
            return
        }
        $(_self).parent().parent().remove();
        if($('#preorderBaseBody tr').length == 0)
        {
            initPreorderTable();
        }
    }

    //添加时间类商品规格
    function addPreorder(_self)
    {
        var html = $(_self).parent().parent().prop("outerHTML");
        $(html).insertAfter($(_self).parent().parent());
    }

    //初始时间类商品规格
    function initPreorderTable()
    {
        //默认产生一条时间类商品标题空挡
        var preorderHeadHtml = template.render('preorderHeadTemplate',{'templateData':[]});
        $('#preorderBaseHead').html(preorderHeadHtml);

        //默认产生一条时间类商品规格空挡
        var preorderRowHtml = template.render('preorderRowTemplate',{'templateData':[[]]});
        $('#preorderBaseBody').html(preorderRowHtml);
    }

    /**
     * 时间类商品会员价格
     * @param obj 按钮所处对象
     */
    function memberPreorderPrice(obj,seller_id,i)
    {
        var sellerId  = seller_id ? seller_id : 0;
        var sellPrice = $(obj).siblings('input[name^="_preorder_price['+i+']"]')[0].value;
        if($.isNumeric(sellPrice) == false)
        {
            alert('请先设置商品的价格再设置会员价格');
            return;
        }

        var groupPriceValue = $(obj).siblings('input[name^="_preorderGroupPrice['+i+']"]');

        //用户组的价格
        art.dialog.data('groupPrice',groupPriceValue.val());

        //开启新页面
        var tempUrl = creatUrl("goods/member_price/sell_price/@sell_price@/seller_id/@seller_id@");
        tempUrl = tempUrl.replace('@sell_price@',sellPrice).replace('@seller_id@',sellerId);
        art.dialog.open(tempUrl,{
            id:'memberPriceWindow',
            title: '会员价格',
            ok:function(iframeWin, topWin)
            {
                var formObject = iframeWin.document.forms['groupPriceForm'];
                var groupPriceObject = {};
                $(formObject).find('input[name^="groupPrice"]').each(function(){
                    if(this.value != '')
                    {
                        //去掉前缀获取group的ID
                        var groupId = this.name.replace('groupPrice','');

                        //拼接json串
                        groupPriceObject[groupId] = this.value;
                    }
                });

                //更新会员价格值
                var temp = [];
                for(var gid in groupPriceObject)
                {
                    temp.push('"'+gid+'":"'+groupPriceObject[gid]+'"');
                }
                groupPriceValue.val('{'+temp.join(',')+'}');
                return true;
            }
        });
    }

</script>